package com.projecy.storehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.projecy.storehouse.dao.ProductUnitDao;
import com.projecy.storehouse.dao.TheLibraryDao;
import com.projecy.storehouse.dao.TheLibraryDefaultDao;
import com.projecy.storehouse.entity.*;
import com.projecy.storehouse.entity.dto.UpdatePutRegister;
import com.projecy.storehouse.entity.dto.UpdatePutRegister2;
import com.projecy.storehouse.service.TheLibraryService;
import com.projecy.storehouse.util.RepertoryNumber;
import com.projecy.storehouse.util.RepertoryResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (TheLibrary)表服务实现类
 *
 * @author makejava
 * @since 2023-03-24 16:46:09
 */
@Service("theLibraryService")
public class TheLibraryServiceImpl extends ServiceImpl<TheLibraryDao, TheLibrary> implements TheLibraryService {

    @Autowired
    TheLibraryDao theLibraryDao;

    @Autowired
    TheLibraryDefaultDao theLibraryDefaultDao;

    @Autowired
    ProductUnitDao productUnitDao;

    /**
     * 添加出库申请
     *
     * @param theLibrary
     * @param list
     * @return
     */
    @Override
    public ResponseEntity<Object> insertTheLibrary(TheLibrary theLibrary, List<TheLibraryDefault> list) {
        boolean bool = false;
        String msg = "";
        //入库编号
        Long aLong = RepertoryNumber.bePutInStorage();
        //修改申请标志
        theLibrary.setOutMark("待申请");
        //循环添加入库明细
        for(int i = 0 ; i < list.size() ; i++){
            TheLibraryDefault theLibraryDefault = list.get(i);
            ProductUnit unit = productUnitDao.selectOne(new QueryWrapper<ProductUnit>().eq("product_id", theLibraryDefault.getProductId()));
            //入库数量不超过仓库上限
            if(theLibraryDefault.getOutDetailAmount()<=unit.getInventoryCeiling()){
                theLibraryDefault.setOutId(aLong);
                theLibraryDefaultDao.insert(theLibraryDefault);
                msg = "添加成功！！";
                bool = true;
            }else{
                //入库数量超过仓库上限
                msg = "数量超出库存设置！！";
                bool = true;
                break;
            }
        }
        int insert = theLibraryDao.insert(theLibrary);
        if(insert==0){
            msg = "添加有误，请重写添加！！";
            bool = false;
        }
        if(bool){
            return RepertoryResultUtil.success(bool,msg);
        }else {
            return RepertoryResultUtil.fail(bool,msg);
        }
    }





    /**
     * 入库申请查询
     * @param mark
     * @return
     */
    @Override
    public ResponseEntity<Object> seacherTheLibrary(String mark) {
        List<TheLibrary> outStorages = theLibraryDao.selectList(new QueryWrapper<TheLibrary>().eq("put_mark",mark));
        return RepertoryResultUtil.success(true,outStorages);
    }



    /**
     * 修改出库申请标志
     * @param mark
     * @return
     */
    @Override
    public ResponseEntity<Object> updateTheLibraryMark(Long outId,String mark) {
        TheLibrary theLibrary = new TheLibrary();
        theLibrary.setOutId(outId);
        theLibrary.setOutMark(mark);
        int i = theLibraryDao.updateById(theLibrary);
        if(i>0){
            return RepertoryResultUtil.success(true,"成功");
        }else {
            return RepertoryResultUtil.fail(false,"失败");
        }
    }


    /**
     * 入库申请查询(状态)
     * @param mark
     * @return
     */
    @Override
    public ResponseEntity<Object> seacherTheLibraryMark(String mark){
        //查询条件
        QueryWrapper<TheLibrary> wrapper = new QueryWrapper<>();
        wrapper.eq("put_mark",mark);
        return RepertoryResultUtil.success(true,theLibraryDao.selectList(wrapper));
    }






    /**
     * 入库调度
     */

    /**
     * 入库调度查询入库理由
     * @param mark
     * @param reason
     * @return
     */
    public ResponseEntity<Object> seacherTheLibraryDispatchMark(String reason,String mark){
        //查询条件
        QueryWrapper<TheLibrary> wrapper = new QueryWrapper<>();
        //调度状态
        wrapper.eq("put_dispatch_mark",mark);
        //配置内容
        wrapper.eq("put_reason",reason);
        return RepertoryResultUtil.success(true,theLibraryDao.selectList(wrapper));
    }

    /**
     *点击调度(单个)
     * @param productId
     * @return
     */
    @Override
    public ResponseEntity<Object> checkTheLibraryDispatch(Long productId) {
        TheLibraryDefault aDefault = new TheLibraryDefault();
        aDefault.setOutDetailMark("已调度");
        return RepertoryResultUtil.success(true,theLibraryDefaultDao.update(aDefault,new QueryWrapper<TheLibraryDefault>().eq("product_id",productId)));
    }

    /**
     *总调度
     * @param outId
     * @return
     */
    @Override
    public ResponseEntity<Object> checkTheLibraryDispatchAll(Long outId,String uname) {
        //明细总数
        long num = theLibraryDefaultDao.selectCount(new QueryWrapper<TheLibraryDefault>().eq("out_id",outId));
        //已调度的数
        long num2 = theLibraryDefaultDao.selectCount(new QueryWrapper<TheLibraryDefault>().eq("out_id", outId).eq("out_detail_mark", "已调度"));

        TheLibrary theLibrary = new TheLibrary();
        if (num==num2){
            theLibrary.setOutDispatchMark("已调度");
            theLibrary.setOutDispatch(uname);
            theLibrary.setOutDispatchTime(new Date());
            return RepertoryResultUtil.success(true,theLibraryDao.updateById(theLibrary));
        }else{
            return RepertoryResultUtil.success(false,null);
        }
    }






    @Override
    public ResponseEntity<Object> updateOutRegister(UpdatePutRegister updatePutRegister) {
        TheLibrary theLibrary = new TheLibrary();
        theLibrary.setOutRegister(updatePutRegister.getName());
        theLibrary.setOutRegisterMark("待登记");
        int pu = theLibraryDao.update(theLibrary, new QueryWrapper<TheLibrary>().eq("out_id", updatePutRegister.getPutId()));
        TheLibraryDefault theLibraryDefault = new TheLibraryDefault();
        List<UpdatePutRegister2> list = updatePutRegister.getList();
        int pr = 0;
        for (int i = 0; i < list.size(); i++) {
            UpdatePutRegister2 updatePutRegister2 = list.get(i);
            theLibraryDefault.setOutDetailAffirm(updatePutRegister2.getNum());
            pr = theLibraryDefaultDao.update(theLibraryDefault, new QueryWrapper<TheLibraryDefault>().eq("out_id", updatePutRegister.getPutId()).eq("product_id", updatePutRegister2.getProductId()));
        };
        if(pu>0 && pr>0){
            return RepertoryResultUtil.success(true,"成功");
        }else{
            return RepertoryResultUtil.success(false,"失败");
        }
    }

    @Override
    public ResponseEntity<Object> outCheckAndAdd(Long putId) {
        TheLibrary theLibrary = new TheLibrary();
        theLibrary.setOutRegisterMark("已登记");
        theLibrary.setOutWarehousingMark("已入库");
        int pu = theLibraryDao.update(theLibrary, new QueryWrapper<TheLibrary>().eq("out_id",putId));
        List<TheLibraryDefault> list = theLibraryDefaultDao.selectList(new QueryWrapper<TheLibraryDefault>().eq("put_id", pu));
        int product_id = 0;
        for (int i = 0; i < list.size(); i++) {
            TheLibraryDefault aDefault = list.get(i);
            ProductUnit one = productUnitDao.selectOne(new QueryWrapper<ProductUnit>().eq("product_id", aDefault.getProductId()));
            ProductUnit unit = new ProductUnit();
            unit.setReserves(unit.getReserves()-aDefault.getOutDetailAffirm());
            product_id = productUnitDao.update(unit, new QueryWrapper<ProductUnit>().eq("product_id", aDefault.getProductId()));
        }
        if(pu>0 && product_id>0){
            return RepertoryResultUtil.success(true,"成功");
        }else{
            return RepertoryResultUtil.success(false,"失败");
        }
    }


}

